import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

//路由表
const routes = [{
    path: '/',
    name: 'login',
    component: () => import('@/views/login/'),
  },
  {
    path: '/layout',
    component: () => import('@/views/layout/'),
    children: [{
      path: '', //默认子路由
      name: 'home',
      component: () => import('@/views/home/'),
    }, {
      path: '/addurl',
      name: 'addurl',
      component: () => import('@/views/addurl/'),
    }, {
      path: '/adminEdit',
      name: 'adminEdit',
      component: () => import('@/views/editCourse/'),
    }, {
      path: '/edit/:token',
      name: 'edit',
      component: () => import('@/views/editCourse/'),
    }, {
      path: '/adminSemester',
      name: 'semester',
      component: () => import('@/views/editSemester/'),
    }, {
      path: '/adminClass',
      name: 'class',
      component: () => import('@/views/editClass/'),
    }, {
      path: '/adminMajor',
      name: 'major',
      component: () => import('@/views/editMajor/'),
    }]
  }
]

const router = new VueRouter({
  mode: 'history',
  // base: '/expschedule/',
  routes
})

//路由导航守卫，所有的页面的导航都会经过这里
router.beforeEach((to, from, next) => {
  // 如果访问的页面不是/，校验登陆状态
  if (window.location.href.indexOf('edit') == -1) {
    // console.log(window.location.pathname);
    const path = window.location.pathname
    const user = JSON.parse(window.localStorage.getItem('USER_KEY'))
    if (to.path !== path) {
      if (user) {
        next()
        return
      } else {
        next(path)
        return
      }
    }
  }
  next()
})

export default router